﻿Imports System.Diagnostics
Imports Microsoft.VisualBasic
Imports TwitterModel
Imports System.Xml
Imports System.ServiceModel.Syndication
Imports System.Data

Public Class Helper
    Public Shared Function Update(ByVal ctx As TwitterEntities, ByVal mac As String, ByVal ex As String, ByVal tweets As List(Of Tweet)) As Integer
        Dim i As Integer = 0

        Dim phone As Phone = ctx.Phones.FirstOrDefault(Function(p) (p.Mac = mac))
        If IsNothing(phone) Then
            phone = New Phone() With {.Mac = mac}
            ctx.Phones.AddObject(phone)
        End If

        Dim PhoneSearch As Search = phone.Searches.FirstOrDefault(Function(s) (s.Expression = ex))
        If IsNothing(PhoneSearch) Then
            PhoneSearch = New Search() With {.Expression = ex}
            phone.Searches.Add(PhoneSearch)
        End If

        For Each tweet As Tweet In tweets

            Dim k = ctx.CreateEntityKey("Tweets", tweet)
            Dim t As Tweet = Nothing
            Dim exists = ctx.TryGetObjectByKey(k, t)

            If exists Then
                tweet = t
            End If

            If Not PhoneSearch.Tweets.Contains(tweet) Then
                PhoneSearch.Tweets.Add(tweet)
            End If
        Next

        Try
            i = ctx.SaveChanges
        Catch e As Exception
            Debug.WriteLine(e.InnerException.ToString)
        End Try

        Return i
    End Function

    Public Shared Function GetTweetsFromTwitter(ByVal ex As String) As Object
        Dim SearchUri As String = "http://search.twitter.com/search.atom?q={0}"
        Dim url As String = String.Format(SearchUri, HttpUtility.UrlEncode(ex))
        Dim rdr As XmlReader = XmlReader.Create(url)
        Dim feed As SyndicationFeed = SyndicationFeed.Load(rdr)

        Dim tweets As New List(Of Tweet)
        For Each item In feed.Items

            Dim tweet As New Tweet() With {.Author = item.Authors.Item(0).Name,
                                            .Id = GetTweetId(item.Id),
                                            .Msg = item.Title.Text,
                                            .PublishDate = item.PublishDate.DateTime.ToLocalTime(),
                                            .BitmapUrl = item.Links(1).Uri.ToString}

            tweets.Add(tweet)

        Next item

        rdr.Close()

        Return tweets
    End Function

    Private Shared Function GetTweetId(ByVal twitterId As String) As String
        Dim parts() As String = twitterId.Split(":".ToCharArray())
        Return parts(2).ToString()
    End Function

    Public Shared Function TweetDetailUrl(ByVal page As Page, ByVal mac As String, ByVal ex As String, ByVal id As String) As String
        Dim Request As HttpRequest = HttpContext.Current.Request
        Dim BaseSiteUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) + page.ResolveUrl(Request.ApplicationPath).TrimEnd("/")
        Dim Url As String = String.Format("{0}/t.aspx?m={1}&ex={2}&id={3}", BaseSiteUrl, mac, ex, id)
        Return Url
    End Function

    Public Shared Function TweetsListUrl(ByVal page As Page) As String
        Dim Request As HttpRequest = HttpContext.Current.Request
        Dim BaseSiteUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) + page.ResolveUrl(Request.ApplicationPath).TrimEnd("/")
        Dim Url As String = String.Format("{0}/tl.aspx", BaseSiteUrl)
        Return Url
    End Function


    Public Shared Function TweetsListUrl(ByVal page As Page, ByVal mac As String, ByVal ex As String, ByVal id As String) As String
        Dim Request As HttpRequest = HttpContext.Current.Request
        Dim BaseSiteUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) + page.ResolveUrl(Request.ApplicationPath).TrimEnd("/")
        Dim Url As String = String.Format("{0}/tl.aspx?m={1}&ex={2}&id={3}", BaseSiteUrl, mac, ex, id)
        Return Url
    End Function


    Public Shared Function TweetSearchUrl(ByVal page As Page, ByVal ex As String) As String
        Dim Request As HttpRequest = HttpContext.Current.Request
        Dim BaseSiteUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) + page.ResolveUrl(Request.ApplicationPath).TrimEnd("/")
        Dim Url As String = String.Format("{0}/ts.aspx?ex={1}", BaseSiteUrl, ex)
        Return Url
    End Function

    Public Shared Function TweetMenuUrl(ByVal page As Page) As String
        Dim Request As HttpRequest = HttpContext.Current.Request
        Dim BaseSiteUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) + page.ResolveUrl(Request.ApplicationPath).TrimEnd("/")
        Dim Url As String = String.Format("{0}/tm.aspx", BaseSiteUrl)
        Return Url
    End Function

    Public Shared Function TweetHistoryUrl(ByVal page As Page, ByVal mac As String) As String
        Dim Request As HttpRequest = HttpContext.Current.Request
        Dim BaseSiteUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) + page.ResolveUrl(Request.ApplicationPath).TrimEnd("/")
        Dim Url As String = String.Format("{0}/th.aspx?m={1}", BaseSiteUrl, mac)
        Return Url
    End Function




    Public Sub GetSnomBitmap()

    End Sub

    Shared Function GetSnomModel(ByVal userAgent As String) As String
        Dim model As String = String.Empty
        Dim regex As New Regex("snom(370|820|821|870)-SIP")
        Dim m As Match = regex.Match(userAgent)
        If m.Success Then
            model = m.Groups(1).Value
        End If
        Return model
    End Function

End Class
